import os
import subprocess
import time

from pymouse import PyMouse # https://github.com/SavinaRoja/PyUserInput

mouse = PyMouse()
x = 1320
y = 200
mouse.move(x, y)
mouse.click(x, y, 1)

for i in range(377):
    page = i + 1
    print(page)
    file_name = "/Users/ddy/Desktop/18/" + str(page) + ".png"
    time.sleep(1)
    cmd = "screencapture -R 345,219,791,1145 " + file_name
    subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    time.sleep(1)
    while True:
        if os.path.exists(file_name):
            break
        else:
            print("wait for file ", page)
            time.sleep(1)
    mouse.click(x, y, 1)
